package com.longxi.tianjintianbao.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AlertDialog;
import android.view.WindowManager;

import com.longxi.tianjintianbao.ui.activity.main.MainActivity;
import com.longxi.tianjintianbao.utils.MyUtils;

/**
 * Created by 70339 on 2018/5/18.
 * 代码无八阿哥
 * 阿弥陀佛
 */
public class MyBroadcastReceiver extends BroadcastReceiver {
    AlertDialog.Builder alert;
    AlertDialog alertDialog;
    boolean isShowDialog = false;
    boolean initDialog = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        switch (action) {
            case "action.PatrolMessageReceiver":
                MainActivity mainActivity = (MainActivity)context;
                mainActivity.refreshPatrolMessage();
                break;
            case "android.net.conn.CONNECTIVITY_CHANGE":
                ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isAvailable()) {
//                    hideDialog();
                } else {
                    MyUtils.showToast("当前网络不可用");
//                    if(!initDialog){
//                        initDialog(context);
//                    }
//                    if(!isShowDialog){
//                        showDialog();
//                    }
                }
                break;
        }
    }

    public void initDialog(Context context){
        alert = new AlertDialog.Builder(context);
        alert.setTitle("温馨提示");
        alert.setMessage("当前网络不可用，请检测您的网络状况");
        alert.setCancelable(false);
        alert.setPositiveButton("网络设置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 跳转到系统的网络设置界面
                Intent intent = new Intent();
                // 先判断当前系统版本
                if(android.os.Build.VERSION.SDK_INT > 10){  // 3.0以上
                    intent.setClassName("com.android.settings", "com.android.settings.Settings");
                }else {
                    intent.setClassName("com.android.phone"
                            , "com.android.phone.NetworkSetting");
                }
                context.startActivity(intent);
            }
        });
        alertDialog = alert.create();
        initDialog = true;
    }

    public void showDialog(){
        isShowDialog = true;
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        if(!alertDialog.isShowing()){
            alertDialog.show();
        }
    }

    public void hideDialog(){
        if(alertDialog == null){
            return;
        }
        if(alertDialog.isShowing()){
            MyUtils.showToast("网络已连接");
            alertDialog.dismiss();
            isShowDialog = false;
        }
    }
}
